﻿using System;

namespace NanoMvvm
{
    public class ResourceProvider
    {
        private Type resourcesClassType;

        public ResourceProvider()
        {
        }

        public Type ResourcesClassType
        {
            get
            {
                return resourcesClassType;
            }
            set
            {
                resourcesClassType = value;
                InitResourcesType(value);
            }
        }

        public TypeViewModel ResourcesViewModel { get; private set; }

        private void InitResourcesType(Type type)
        {
            ResourcesViewModel = new TypeViewModel(type);

            ResourcesViewModel.RaisePropertiesChanged();

            if (NanoMvvmFramework.IsInitialized)
            {
                NanoMvvmFramework.Instance.Localization.CultureChanged += OnCultureChanged;
            }
            else
            {
                NanoMvvmFramework.FrameworkInitialized += OnFrameworkInitialized;
            }
        }

        private void OnFrameworkInitialized(object sender, EventArgs e)
        {
            NanoMvvmFramework.Instance.Localization.CultureChanged += OnCultureChanged;
            ResourcesViewModel.RaisePropertiesChanged();
        }

        private void OnCultureChanged(object sender, EventArgs e)
        {
            ResourcesViewModel.RaisePropertiesChanged();
        }
    }
}